#ifndef TCPCLIENT_H
#define TCPCLIENT_H

#include <QTcpSocket>
#include <QTimer>

class TcpClient : public QTcpSocket
{
    Q_OBJECT
public:
    explicit TcpClient(QObject *parent = nullptr);
    ~TcpClient();

    void connectToServer(const QString &host, quint16 port);
    void sendData(const QByteArray &data);

signals:
    void dataReceived(const QByteArray &data);
    void errorOccurred(const QString &errorString);
    void connected();
    void disconnected();
    void connectionFailed(const QString &errorString);

private slots:
    virtual void onReadyRead();
    void onError(QAbstractSocket::SocketError error);
    void onDisconnected();
    void reconnect();
    void onConnectTimeout();

private:
    QString m_host;
    quint16 m_port;
    QTimer m_reconnectTimer;  // Timer to handle reconnection attempts
    QTimer m_connectTimeoutTimer;  // Timer for connection timeout
    int m_reconnectInterval;  // Current reconnect interval
    const int m_initialReconnectInterval = 5000;  // Initial reconnect interval in milliseconds
    const int m_maxReconnectInterval = 60000;  // Maximum reconnect interval in milliseconds
};

#endif // TCPCLIENT_H
